import mock from "./utils/mock";

//app.js
App({
  onLaunch: function (options) {
    Object.assign(this.globalData,mock)
    // console.log(this.globalData);

    var that = this;
    that.globalData.qrstr = '';

    if(options){
      if (options.query) {
        if(options.query.query){
          that.globalData.qrstr = options.query.query;
        }
        if(options.query.str){
          if(that.globalData.qrstr==''){
            that.globalData.qrstr = options.query.str;
          } 
        }
      }
    }

	
	  const updateManager = wx.getUpdateManager()
    updateManager.onCheckForUpdate(function (res) {
      // console.log(res.hasUpdate);          // 请求完新版本信息的回调
    })

    updateManager.onUpdateReady(function () {
      wx.showModal({
        title: '更新提示',
        content: '新版本已经准备好，是否重启应用？',
        success: function (res) {
          if (res.confirm) {
            updateManager.applyUpdate();     // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
          }
        }
      })
    })

    updateManager.onUpdateFailed(function () {
      // 新的版本下载失败
    })

    that.getUserInfo();

  },

  
  //购物车数量
  cart_count(){
    var e = this
    if(e.globalData.userInfo){
      wx.request({
        url: e.globalData.httpUrl+"api/mini/cart_count",
        header: {
          'content-type': 'application/x-www-form-urlencoded','appid':e.globalData.appId
        },
        method: 'POST',
        dataType: 'json',
        data: {
          user_id: e.globalData.userInfo.id
        },
        success: function(re) {
          console.log(re)
          wx.setTabBarBadge({
            index: 2,
            text: ''+re.data+''
          })
        }
      })
    }
    
  },
		
	

  



  //获取用户信息
  getUserInfo: function(sn='') { 
    var e = this;
    console.log(sn);
		wx.login({
			success: function(a) {  
        
        wx.request({
					url: e.globalData.httpUrl+"api/mini/getOpenid",
					header: {
						'content-type': 'application/x-www-form-urlencoded','appid':e.globalData.appId
					},
					method: 'POST',
					dataType: 'json',
					data: {
						code: a.code
					},
					success: function(re) {
            var openid  = 	re.data.data.openid;
            if(re.data.data.unionid){
              var unionid = 	re.data.data.unionid;
            }else{
              var unionid = 	'';
            }
						wx.hideLoading() ;
						console.log("openid信息",openid ), getApp().getOpenId = openid,
							wx.request({
								url: e.globalData.httpUrl+"api/mini/getwechatuser",
								data: {
                  openid: openid,
                  sn:sn,
                  unionid:unionid
								},
								header: {
									'content-type': 'application/x-www-form-urlencoded','appid':e.globalData.appId
								},
								method: 'POST',
								dataType: 'json',
								success: function(a) {   //console.log(a.data.data);
									wx.setStorageSync("users", a.data.data),  
                  e.globalData.userInfo = a.data.data;
                  console.log(e.globalData);
                  e.cart_count()
								}
							});
					},
					fail: function(a) {},
					complete: function(a) {}
				});
			}
		});
  },
  
  globalData: {
    userInfo: null,
    //接口URL地址以及图片展现地址
    httpUrl:'https://c.shimeng.net.cn/',
    apiurl:'https://c.shimeng.net.cn/',
    baseurl:"https://c.shimeng.net.cn/",
    //APPID
    appId:'wx1c4350fdd6c24b9a',
    //订单
    order_type:2,
    //二维码信息
    qrstr:'',
    //导航
    nav:0
  }
})